package net.alan.modules.aspect;

import net.alan.modules.cache.WebContextUtil;
import net.alan.modules.handle.LoginExecption;
import net.alan.modules.handle.RoleExecption;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

/**
 * Author 李岩飞
 * Email eliyanfei@126.com
 * 2017/11/21
 */
@Aspect
@Component
public class HttpAspect {

    private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    @Pointcut("execution(public * net.alan.modules.controller.*Controller.*(..))")
    public void login1() {
    }
    @Pointcut("@annotation(NotAuthorize)")
    public void login2() {
    }

    /**
     * 判断是否登录
     * @param joinPoint
     * @throws RoleExecption
     */
    @Before(value = "login1() && !login2()")
    public void beforeLogin(JoinPoint joinPoint) throws LoginExecption {
        if(!WebContextUtil.isLogin()){
            throw new LoginExecption("没有登录");
        }
    }


    @Pointcut("@annotation(AdminRole)")
    public void adminRole() {
    }

    /**
     * 所有方法上面有AdminRole注解的，进入方法前都会进入改方法统一判断是否有权限，如果没有抛出异常
     * @param joinPoint
     * @throws RoleExecption
     */
    @Before("adminRole()")
    public void beforeAdmin(JoinPoint joinPoint) throws RoleExecption{
        if(!WebContextUtil.isAdmin()){
            throw new RoleExecption("没有权限操作");
        }
    }
}
